classdef spectrum2D
    properties
        z1 = [];
        z2 = [];
        z3 = [];
        z4 = [];
        mode = 0;
    end
    
    methods
        function s = spectrum2D(varargin)          
            if(nargin==0)
                % Do nothing
            elseif(nargin==1)
                if(ischar(varargin{1}))
                    % SYNTAX: sp = spectrum2D('DataFile.dat')
                    filename = varargin{1};
                    fid = fopen(filename);
                    if(fid<0)% open failed
                        error('Failed to open %s\n',filename);
                    end
                    data = textscan(fid,'%f %f %f %f');
                    fclose(fid);
                    mode = sqrt(length(data{1}));
                    s.mode = mode;
                    assert((mode*mode==length(data{1})),'Error when construct spectrum2d');
                    s.z1 = zeros(mode+1,mode+1);
                    s.z2 = zeros(mode+1,mode+1);
                    s.z3 = zeros(mode+1,mode+1);
                    s.z4 = zeros(mode+1,mode+1);
                    
                    z1_temp = data{1};
                    z2_temp = data{2};
                    z3_temp = data{3};
                    z4_temp = data{4};
                    for m = 1:(mode+1)
                        s.z1(m,:) = z1_temp((m-1)*mode:m*mode-1)';
                        s.z2(m,:) = z2_temp((m-1)*mode:m*mode-1)';
                        s.z3(m,:) = z3_temp((m-1)*mode:m*mode-1)';
                        s.z4(m,:) = z4_temp((m-1)*mode:m*mode-1)';
                    end
                elseif(isnumeric(varargin{1}))
                    % SYNTAX: sp = spectrum2D(20)
                    mode = varargin{1};
                    s.mode = mode;
                    s.z1 = zeros(mode+1,mode+1);
                    s.z2 = zeros(mode+1,mode+1);
                    s.z3 = zeros(mode+1,mode+1);
                    s.z4 = zeros(mode+1,mode+1);
                end
            else
                error('Too many inputs');
            end
        end
        
        function h = stem3(obj)
            h = gcf;
            subplot(2,2,1);
            stem3(obj.z1);
            xlabel('m');
            ylabel('n');
            title('\phi_1')
            
            subplot(2,2,2);
            stem3(obj.z2);
            title('\phi_2');
            xlabel('m');
            ylabel('n');
            
            subplot(2,2,3);
            stem3(obj.z3);
            title('\phi_3');
            xlabel('m');
            ylabel('n');
            
            subplot(2,2,4);
            stem3(obj.z4);
            title('\phi_4');
            xlabel('m');
            ylabel('n');            
        end
        
        function h = mesh(obj)
            h = gcf;
            
            subplot(2,2,1);
            mesh(obj.z1);
            title('\phi_1')
            xlabel('m');
            ylabel('n');
            
            subplot(2,2,2);
            mesh(obj.z2);
            title('\phi_2');
            xlabel('m');
            ylabel('n');
            
            subplot(2,2,3);
            mesh(obj.z3);
            xlabel('m');
            ylabel('n');
            title('\phi_3');
            
            subplot(2,2,4);
            mesh(obj.z4);
            title('\phi_4');
            xlabel('m');
            ylabel('n');
        end
        
        function sp2 = aggregate(sp,A)
            % This function modify the spectrum of 
            sp2 = spectrum2D(floor(sp.mode/A));
            sp2.z1 = sp.z1(1:sp2.mode,1:sp2.mode);
            sp2.z2 = sp.z2(1:sp2.mode,1:sp2.mode);
            sp2.z3 = sp.z3(1:sp2.mode,1:sp2.mode);
            sp2.z4 = sp.z4(1:sp2.mode,1:sp2.mode);
            
            
        end
        
%         function r2 = get.r2(s)
%             r2 = sum((s.z1).^2+(s.z2).^2+(s.z3).^2+(s.z4).^2);
%         end
          
    end
end

%         function h = disp(s)
%             h = gcf;
%             subplot(2,2,1);
%             stem3(s.z1);
%             title('phi_1')
%             subplot(2,2,2);
%             stem3(s.z2);
%             title('phi_2');
%             subplot(2,2,3);
%             stem3(s.z3);
%             title('phi_3');
%             subplot(2,2,4);
%             stem3(s.z4);
%             title('phi_4');            
%         end

